﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace bgp8.Controls
{

    public sealed partial class Die : GamePiece
    {

        private static readonly float tumbleDistance = 200.0F;

        public Die()
        {
            this.InitializeComponent();
            this.Width = 80;
            this.Height = 80;
            NumFaces = 6;
            distance = 0.0F;
            random = new Random((int)DateTime.Now.ToBinary());
            ManipulationDelta += Die_ManipulationDelta;
            ManipulationCompleted += Die_ManipulationCompleted;
        }
        public Die(int faces) 
        {
            this.InitializeComponent();
            this.Width = 80;
            this.Height = 80;
            NumFaces = faces;
            distance = 0.0F;
            random = new Random((int)DateTime.Now.ToBinary());
            ManipulationDelta += Die_ManipulationDelta;
            ManipulationCompleted += Die_ManipulationCompleted;
        }

        public override GamePiece Clone()
        {
            return new Die(NumFaces);
        }

        void Die_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
        {
            distance = 0.0F;
        }

        private float distance;

        void Die_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            if (e.IsInertial)
            {
                distance += (float)Math.Sqrt((Math.Pow(e.Delta.Translation.X * this.ManipulationManager.CurrentTranslationModifierX, 2.0) + Math.Pow(e.Delta.Translation.Y * this.ManipulationManager.CurrentTranslationModifierY, 2.0)));
                if (distance / tumbleDistance >= 1.0)
                {
                    distance = distance % tumbleDistance;
                    TumbleDie();
                }
            }
        }

        public DependencyProperty FaceTextProperty = DependencyProperty.Register("FaceText", typeof(String), typeof(Die), null);

        public String FaceText 
        {
            get { return (String)GetValue(FaceTextProperty); }
            set { SetValue(FaceTextProperty, value); }
        }

        public DependencyProperty MaxFacesTextProperty = DependencyProperty.Register("MaxFacesText", typeof(String), typeof(Die), null);
        public String MaxFacesText
        {
            get { return (String)GetValue(MaxFacesTextProperty); }
            set { SetValue(MaxFacesTextProperty, value); }
        }

        public void TumbleDie() 
        {
            FaceText = random.Next(1, NumFaces + 1).ToString();
        }


        private Random random;
        private int _numFaces;

        public int NumFaces 
        {
            get { return _numFaces; }
            set 
            {
                _numFaces = value;
                FaceText = NumFaces.ToString();
                MaxFacesText = "D" + NumFaces.ToString();
            } 
        }


    }
}
